﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using YArchitech.LIB;
using YJKRevitKernel.RevitObjectSerializeUtility;
using YJKCopyModel.Models;

namespace YJKCopyModel.Basic
{
	public class SpecialtyXml
	{
		public static string GetXmlPath()
		{
			return Product.DataLocation + "\\QuickModelingData\\SpecialtyConfigs.xml";
		}

		public static List<SpecialtyEquipData> ReadDatasFromXml()
		{
			List<SpecialtyEquipData> result = new List<SpecialtyEquipData>();
			try
			{
				string xmlPath = SpecialtyXml.GetXmlPath();
				SpecialtyConfigs specialtyConfigs = null;
				bool flag = File.Exists(xmlPath);
				if (flag)
				{
					specialtyConfigs = new SpecialtyConfigs();
					HYRevitObjectSerializeManger hyrevitObjectSerializeManger = new HYRevitObjectSerializeManger();
					Exception ex;
					hyrevitObjectSerializeManger.TryGetAPersistentClassByXML<SpecialtyConfigs>(xmlPath, out specialtyConfigs, out ex, true);
					bool flag2 = specialtyConfigs != null;
					if (flag2)
					{
						return specialtyConfigs.SpecialtyEquipDatas;
					}
				}
			}
			catch
			{
			}
			return result;
		}

		public static string GetDuctCoilXmlPath()
		{
			return Product.DataLocation + "\\QuickModelingData\\DuctCoilConfigs.xml";
		}

		public static List<DuctCoilEquipData> ReadDatasFromDuctCoilXml()
		{
			List<DuctCoilEquipData> result = new List<DuctCoilEquipData>();
			try
			{
				string ductCoilXmlPath = SpecialtyXml.GetDuctCoilXmlPath();
				DuctCoilConfigs ductCoilConfigs = null;
				bool flag = File.Exists(ductCoilXmlPath);
				if (flag)
				{
					ductCoilConfigs = new DuctCoilConfigs();
					HYRevitObjectSerializeManger hyrevitObjectSerializeManger = new HYRevitObjectSerializeManger();
					Exception ex;
					hyrevitObjectSerializeManger.TryGetAPersistentClassByXML<DuctCoilConfigs>(ductCoilXmlPath, out ductCoilConfigs, out ex, true);
					bool flag2 = ductCoilConfigs != null;
					if (flag2)
					{
						return ductCoilConfigs.DuctCoilEquipDatas;
					}
				}
			}
			catch
			{
			}
			return result;
		}

		public static List<string> GetAllDuctCoilBlockNames()
		{
			List<DuctCoilEquipData> list = SpecialtyXml.ReadDatasFromDuctCoilXml();
			bool flag = list.IsEmpty<DuctCoilEquipData>();
			List<string> result;
			if (flag)
			{
				result = new List<string>();
			}
			else
			{
				List<string> list2 = new List<string>();
				foreach (DuctCoilEquipData ductCoilEquipData in list)
				{
					bool flag2 = ductCoilEquipData != null && !string.IsNullOrEmpty(ductCoilEquipData.Name) && !string.IsNullOrEmpty(ductCoilEquipData.Name.Trim()) && !list2.Contains(ductCoilEquipData.Name.Trim());
					if (flag2)
					{
						list2.Add(ductCoilEquipData.Name.Trim());
					}
				}
				result = list2;
			}
			return result;
		}

		public static string GetBlockConvertXmlPath()
		{
			return Product.DataLocation + "\\QuickModelingData\\BlockConvertConfigs.xml";
		}

		public static List<BlockConvertCategory> ReadDatasFromBlockConvertXml()
		{
			List<BlockConvertCategory> result = new List<BlockConvertCategory>();
			try
			{
				string blockConvertXmlPath = SpecialtyXml.GetBlockConvertXmlPath();
				BlockConvertConfigs blockConvertConfigs = null;
				bool flag = File.Exists(blockConvertXmlPath);
				if (flag)
				{
					blockConvertConfigs = new BlockConvertConfigs();
					HYRevitObjectSerializeManger hyrevitObjectSerializeManger = new HYRevitObjectSerializeManger();
					Exception ex;
					hyrevitObjectSerializeManger.TryGetAPersistentClassByXML<BlockConvertConfigs>(blockConvertXmlPath, out blockConvertConfigs, out ex, true);
					bool flag2 = blockConvertConfigs != null;
					if (flag2)
					{
						return blockConvertConfigs.CategoryDatas;
					}
				}
			}
			catch
			{
			}
			return result;
		}

		public static List<string> GetBlockConvertCategoryNames()
		{
			List<BlockConvertCategory> list = SpecialtyXml.ReadDatasFromBlockConvertXml();
			bool flag = list.IsEmpty<BlockConvertCategory>();
			List<string> result;
			if (flag)
			{
				result = new List<string>();
			}
			else
			{
				List<string> list2 = new List<string>();
				foreach (BlockConvertCategory blockConvertCategory in list)
				{
					bool flag2 = blockConvertCategory != null && !string.IsNullOrEmpty(blockConvertCategory.Name) && !string.IsNullOrEmpty(blockConvertCategory.Name.Trim()) && !list2.Contains(blockConvertCategory.Name.Trim());
					if (flag2)
					{
						list2.Add(blockConvertCategory.Name.Trim());
					}
				}
				result = list2;
			}
			return result;
		}

		public static string GetWindMouthXmlPath()
		{
			return Product.DataLocation + "\\QuickModelingData\\WindMouthDirectionConfigs.xml";
		}

		public static string ReadWindMouthDirection(string familyName)
		{
			string windMouthXmlPath = SpecialtyXml.GetWindMouthXmlPath();
			XmlDocument xmlDocument = SpecialtyXml.GetXmlDocument(windMouthXmlPath);
			bool flag = xmlDocument == null;
			string result;
			if (flag)
			{
				result = "0";
			}
			else
			{
				XmlNode xmlNode = xmlDocument.SelectSingleNode("Configs");
				bool flag2 = xmlNode == null || xmlNode.ChildNodes == null || xmlNode.ChildNodes.Count <= 0;
				if (flag2)
				{
					result = "0";
				}
				else
				{
					foreach (object obj in xmlNode.ChildNodes)
					{
						XmlNode xmlNode2 = (XmlNode)obj;
						XmlElement xmlElem = xmlNode2 as XmlElement;
						string text = SpecialtyXml.GetAttributeFromXmlNode(xmlElem, "FamilyName").Trim();
						bool flag3 = string.IsNullOrEmpty(text) || text != familyName;
						if (!flag3)
						{
							return SpecialtyXml.GetAttributeFromXmlNode(xmlElem, "Direction").Trim();
						}
					}
					result = "0";
				}
			}
			return result;
		}

		public static void WriteWindMouthDirection(string familyName, string dir)
		{
			try
			{
				string windMouthXmlPath = SpecialtyXml.GetWindMouthXmlPath();
				XmlDocument xmlDocument = SpecialtyXml.GetXmlDocument(windMouthXmlPath);
				bool flag = xmlDocument == null;
				if (!flag)
				{
					XmlNode xmlNode = xmlDocument.SelectSingleNode("Configs");
					bool flag2 = xmlNode == null || xmlNode.ChildNodes == null || xmlNode.ChildNodes.Count <= 0;
					if (!flag2)
					{
						foreach (object obj in xmlNode.ChildNodes)
						{
							XmlNode xmlNode2 = (XmlNode)obj;
							XmlElement xmlElement = xmlNode2 as XmlElement;
							string text = SpecialtyXml.GetAttributeFromXmlNode(xmlElement, "FamilyName").Trim();
							bool flag3 = string.IsNullOrEmpty(text) || text != familyName;
							if (!flag3)
							{
								xmlElement.SetAttribute("Direction", dir);
							}
						}
						xmlDocument.Save(windMouthXmlPath);
					}
				}
			}
			catch
			{
			}
		}

		public static XmlDocument GetXmlDocument(string filePath)
		{
			XmlDocument xmlDocument = new XmlDocument();
			bool flag = !File.Exists(filePath);
			if (flag)
			{
				XmlNode newChild = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
				xmlDocument.AppendChild(newChild);
				XmlElement newChild2 = xmlDocument.CreateElement("Configs");
				XmlNode xmlNode = xmlDocument.AppendChild(newChild2);
			}
			else
			{
				xmlDocument.Load(filePath);
			}
			return xmlDocument;
		}

		public static string GetAttributeFromXmlNode(XmlElement xmlElem, string attribute)
		{
			bool flag = xmlElem == null || !xmlElem.HasAttribute(attribute);
			string result;
			if (flag)
			{
				result = "";
			}
			else
			{
				string attribute2 = xmlElem.GetAttribute(attribute);
				result = (string.IsNullOrEmpty(attribute2) ? "" : attribute2);
			}
			return result;
		}
	}
}
